package com.bjsj.pro.jp;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.bjsj.pro.ui.BannderDetailsActivity;
import com.bjsj.pro.ui.PushTzActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author: 正纬
 * @since: 15/4/9
 * @version: 1.1
 * @feature: 用于接收推送的通知和消息
 */
public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    public static int num=0;
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.d("zzzzzzzzzzzzzz", title + ", summary: " + summary + ", extraMap: " + extraMap);
        num++;
        String systemmoel=Build.BRAND;
        if(systemmoel.equals("vivo")){
            Log.e(TAG, "vivoShortCut....");
            Intent localIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
            localIntent.putExtra("packageName", context.getPackageName());
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            localIntent.putExtra("className", launchClassName);
            if (num==0) {
                num =0;
            } else {
                int numInt = Integer.valueOf(num);
                if (numInt > 0) {
                    if (numInt > 99) {
                        num = 99;
                    }
                } else {
                    num =0;
                }
            }
            localIntent.putExtra("notificationNum", num);
            context.sendBroadcast(localIntent);
        }else if(systemmoel.equals("OPPO")){

        }else if(systemmoel.equals("Huawei")){
            String launcherClassName = "com.redfinger.huaweichangebadge.MainActivity";//启动的Activity完整名称
            Bundle localBundle = new Bundle();//需要存储的数据
            localBundle.putString("package",context.getPackageName());//包名
            localBundle.putString("class",launcherClassName);
            localBundle.putInt("badgenumber",num);//未读信息条数
            context.getContentResolver().call(
                    Uri.parse("content://com.huawei.android.launcher.settings/badge/"),
                    "change badge",null,localBundle);
        }else if(systemmoel.equals("xiaomi")){
            try{
                Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
                Object miuiNotification = miuiNotificationClass.newInstance();
                Field field = miuiNotification.getClass().getDeclaredField("messageCount");
                field.setAccessible(true);
                field.set(miuiNotification,String.valueOf(num==0?"":num));
            }catch (Exception e){
                e.printStackTrace();
                // miui6之前
                Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
                localIntent.putExtra("android.intent.extra.update_application_component_name",context.getPackageName()+"/."+"MainActivity");
                localIntent.putExtra("android.intent.extra.update_application_message_text",String.valueOf(num==0?"":num));
                context.sendBroadcast(localIntent);
            }
        }
    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        // Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
        Log.d("zzzzzzzzzzzzzz", cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());

    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        num=0;
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(extraMap);
            String url = jsonObject.optString("webViewUrl");
            Intent i4 = new Intent(context, PushTzActivity.class);
            i4.putExtra("url", url);
            i4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
            context.startActivity(i4);
           // context.startActivity(new Intent(context, BannderDetailsActivity.class).putExtra("url",url));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        //Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
      //  Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
      //  Log.e("MyMessageReceiver", "onNotificationRemoved");
    }
}